package com.masterclient.app.businessmodule.homemodule.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.old.EventAdDataCallback;
import com.masterclient.app.businessmodule.homemodule.view.holder.HomeViewHolder;
import com.masterclient.app.modules.income.model.HomeDataBean;
import com.masterclient.app.utils.CommonUtil;
import com.youth.banner.Banner;

import java.util.List;


public class OtherMeetAdapter extends DelegateAdapter.Adapter<HomeViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;
    private List<HomeDataBean.HotEventItem>  shCategoryData;
    private OtherMeetItemAdapter otherMeetItemAdapter;
    private EventAdDataCallback eventAdDataCallback;
    private int itemPosition = 0;
    private String title;

    public interface EventAdDataCallback {
        void adData();
        void clickItem(String eventId,String type);
    }

    public void setEventAdDataCallback(EventAdDataCallback eventAdDataCallback) {
        this.eventAdDataCallback = eventAdDataCallback;
    }

    public OtherMeetAdapter(Context context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null, null,"");
    }

    public OtherMeetAdapter(Context context, LayoutHelper layoutHelper,
                            int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams,
                            List<HomeDataBean.HotEventItem>  shCategoryData,String title) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.shCategoryData = shCategoryData;
        this.title = title;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(HomeViewHolder holder, int position, int offsetTotal) {
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        RecyclerView recyclerView = holder.itemView.findViewById(R.id.sh_temporaryRecyclerView);
        TextView tvTitle = holder.itemView.findViewById(R.id.tv_title);
        tvTitle.setText(title);
        //金刚区
        if (shCategoryData != null) {
            recyclerView.setVisibility(View.VISIBLE);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            otherMeetItemAdapter = new OtherMeetItemAdapter();
            otherMeetItemAdapter.bindToRecyclerView(recyclerView);
            otherMeetItemAdapter.setNewData(shCategoryData);
            otherMeetItemAdapter.setEnableLoadMore(true);
            otherMeetItemAdapter.setOnLoadMoreListener(() -> {
                eventAdDataCallback.adData();
            });
            otherMeetItemAdapter.setOnClickStatusListener((eventId, position12) -> {
                itemPosition = position12;
                eventAdDataCallback.clickItem(eventId,"other");
            });
            otherMeetItemAdapter.setOnItemClickListener((adapter, view, position1) -> {
                HomeDataBean.HotEventItem banner = shCategoryData.get(position1);
                CommonUtil.goAnyWhere(mContext,banner.eventUrl,"","","","","");
            });
        }
    }

    public void notifyData(){
        shCategoryData.get(itemPosition).hasSubscribe = "1";
        otherMeetItemAdapter.notifyDataSetChanged();
    }


    public void setLoadMore(boolean loadMore){
        otherMeetItemAdapter.setEnableLoadMore(loadMore);
    }

    public void loadMore(List<HomeDataBean.HotEventItem> data){
        otherMeetItemAdapter.setNewData(data);
        otherMeetItemAdapter.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 4;
    }
}
